package com.zzt.questionbankquestion.service.impl;

import com.zzt.questionbankquestion.bean.Course;
import com.zzt.questionbankquestion.bean.dto.CourseDTO;
import com.zzt.questionbankquestion.bean.dto.PageResult;
import com.zzt.questionbankquestion.bean.dto.Result;
import com.zzt.questionbankquestion.mapper.CourseConverter;
import com.zzt.questionbankquestion.repository.CourseRepository;
import com.zzt.questionbankquestion.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseConverter courseConverter;

    @Autowired
    private CourseRepository courseRepository;

    @Override
    public Result addCourse(CourseDTO dto) {
        Result result=new Result();
        if(dto.getCourseName()==null){
            result.setCode(200);
            result.setMsg("参数不足");
        }
        else {
            if(courseRepository.findByCourseName(dto.getCourseName())==null){
                Course course=courseConverter.dtoToDo(dto);
                courseRepository.save(course);
                result.setCode(200);
                result.setMsg("添加成功");
            }
            else {
                result.setCode(200);
                result.setMsg("课程已存在");
            }
        }
        return result;
    }

    @Override
    public Result editCourse(CourseDTO dto) {
        Result result=new Result();
        if(dto.getCourseName()==null||dto.getId()==0){
            result.setCode(200);
            result.setMsg("参数不齐");
        }
        else {
            if(courseRepository.findById(dto.getId())!=null){
                Course course=courseConverter.dtoToDo(dto);
                courseRepository.save(course);
                result.setCode(200);
                result.setMsg("修改成功");
            }
            else {
                result.setCode(200);
                result.setMsg("课程不存在，修改失败");
            }
        }
        return result;
    }

    @Override
    public Result deleteCourse(Long courseId) {
        Result result=new Result();
        if(courseRepository.findById(courseId).isPresent()){
            courseRepository.deleteById(courseId);
            result.setCode(200);
            result.setMsg("删除成功");
        }
        else {
            result.setCode(200);
            result.setMsg("课程不存在，删除失败");
        }
        return result;
    }

    @Override
    public Result< CourseDTO > get(Long courseId) {
        Result<CourseDTO> result=new Result();
        Course course=courseRepository.findById(courseId).get();
        if(course==null){
            result.setCode(200);
            result.setMsg("课程不存在，获取失败");
        }
        else {
            CourseDTO dto=courseConverter.doToDTO(course);
            result.setData(dto);
            result.setCode(200);
            result.setMsg("获取成功");
        }
        return result;
    }

    @Override
    public PageResult< CourseDTO > getAPage(PageRequest pageRequest) {
        PageResult<CourseDTO> pageResult=new PageResult<>();
        Page<Course> page=courseRepository.findAll(pageRequest);
        List<CourseDTO> dtos=courseConverter.doToDTO(page.getContent());
        pageResult.setCount(page.getTotalElements());
        pageResult.setPage(page.getTotalPages());
        pageResult.setResult(dtos);
        return pageResult;
    }

    @Override
    public PageResult< CourseDTO > getASpecialPage(Specification< Course > specification, PageRequest pageRequest) {
        PageResult<CourseDTO> pageResult=new PageResult<>();
        Page<Course> page=courseRepository.findAll(specification,pageRequest);
        List<CourseDTO> dtos=courseConverter.doToDTO(page.getContent());
        pageResult.setCount(page.getTotalElements());
        pageResult.setPage(page.getTotalPages());
        pageResult.setResult(dtos);
        return pageResult;
    }
}
